package com.oceansoft.core.rcgz.dao.impl;

import com.hui.platform.system.exception.HuiException;
import com.oceansoft.core.rcgz.dao.IRcgzDao;
import com.oceansoft.core.rcgz.domain.Dailywork;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by OS on 2017/12/1.
 */
@Repository
public class RcgzDaoImpl implements IRcgzDao{
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public Dailywork dailyWorkContent(String policeNum, String workdate) throws HuiException {
        String sql = "select * from t_jxkh_dailylog t where 1=1 ";
        if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(workdate)){
            sql+=" and t.workdate = to_date('"+workdate+"','YYYY-MM-DD')";
        }
        if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(policeNum)){
            sql+=" AND t.policenum ='"+policeNum+"'";
        }

        List<Dailywork> list = jdbcTemplate.query(sql, new RowMapper<Dailywork>() {
            @Override
            public Dailywork mapRow(ResultSet rs, int arg1) throws SQLException {
                Dailywork dailywork = new Dailywork();
                dailywork.setContent1(rs.getString("CONTENT1"));
                dailywork.setContent2(rs.getString("CONTENT2"));
                dailywork.setContent3(rs.getString("CONTENT3"));
                dailywork.setPoliceNum(rs.getString("policeNum"));
                dailywork.setWorkdate(rs.getString("workdate"));
                dailywork.setDbid(rs.getInt("dbid"));
                dailywork.setCreatetime(rs.getString("createTime"));
                return dailywork;
            }
        });
        if(list.size()!=0)
            return list.get(0);
        else
            return new Dailywork();
    }

    @Override
    public void addWorkContent(Dailywork dailywork) {
        String sql = "insert into t_jxkh_dailylog_1 (dbid,policeNum,workdate,content1,content2,content3) values(SEQ_t_jxkh_dailylog_1.NEXTVAL,?,to_date(?,'YYYY-MM-DD'),?,?,?)";
        jdbcTemplate.update(sql,dailywork.getPoliceNum(),dailywork.getWorkdate(),dailywork.getContent1(),dailywork.getContent2(),dailywork.getContent3());
    }

    @Override
    public void editWorkContent(Dailywork dailywork) {
        String sql = "update t_jxkh_dailylog_1 set content1 = ? , content2 = ? , content3 = ? where policeNum = ? and workdate = to_date(?,'YYYY-MM-DD')";
        jdbcTemplate.update(sql,dailywork.getContent1(),dailywork.getContent2(),dailywork.getContent3(),dailywork.getPoliceNum(),dailywork.getWorkdate());
    }
}
